package de.koller.million.render.obj;

import de.koller.million.render.RenderRoot;
import de.koller.million.render.obj.base.Cube;
import de.koller.million.render.util.Buffer.IGenerate;
import de.koller.oceanFramework.gl.util.ParticleTexture;
import de.koller.oceanFramework.gl.util.TextureUtil;
import de.koller.oceanFramework.tree.Mother;
import de.koller.oceanFramework.tree.Sea3X;
import de.koller.oceanFramework.util.ColorIntUtil;
import java.util.Random;
import java.util.StringTokenizer;
import org.lwjgl.opengl.GL11;

public class GlowHouse extends Sea3X {
	
	private static final Random rnd = new Random();
	@Mother private RenderRoot rroot;
	
	private final float x, z, h, w;
	private int facade;
	
	public GlowHouse( float x, float z, float h ) {
		this.x = x;
		this.z = z;
		this.h = h;
		this.w = 1.5f;
	}

	@Override
	protected void init() {
		super.init();
		
		facade = rroot.buffer.bufferI.getContent( "facade " + rnd.nextInt( 7 ), new FacadeGenerate() );
	}

	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		TextureUtil.bind( facade );
		
		GL11.glPushMatrix();
		GL11.glTranslatef( x, h/2, z );
		new Cube( rroot, w, w, h ).renderThis();
		GL11.glPopMatrix();
	}
	
	private static class FacadeGenerate implements IGenerate<Integer> {
		private static final Random rnd = new Random();
		
		private int color( int n ) {
			switch( n ) {
				case 0: return 0xff_0000ff;
				case 1: return 0xff_00ff00;
				case 2: return 0xff_00ffff;
				case 3: return 0xff_ff0000;
				case 4: return 0xff_ff00ff;
				case 5: return 0xff_ffff00;
				case 6: return 0xff_ffffff;
			}
			throw new IllegalArgumentException( n + " no color!" );
		}
		
		@Override
		public Integer create( String key ) {
			int size = 64;
			ParticleTexture t = ParticleTexture.generate( size, size );
			for( int x = 0; x < size; x++ )
				for( int y = 0; y < size; y++ ) {
					int f = 20 + rnd.nextInt( 50 );
					t.setPixel( ColorIntUtil.getIntColor( 0xff, f, f, f ), x, y );
				}
			
			StringTokenizer st = new StringTokenizer( key );
			st.nextToken();
			int c = color( Integer.parseInt( st.nextToken() ) );
			
			for( int i = 0; i < size; i++ ) {
				t.setPixel( c, i, 0 );
			}
				
			t.flush();
			
			TextureUtil.MAG_FILTER = GL11.GL_NEAREST;
			int id = TextureUtil.makeMipMap( t );
			TextureUtil.delete( t.getTextureID() );
			return id;
		}
	
	}
	
}
